package com.wxzz.elearing.course.api.app;

import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.course.dto.ExamByWxUserIdDto;
import com.wxzz.elearing.course.dto.ExamDto;
import com.wxzz.elearing.course.dto.WxUserExamHistoryDto;
import com.wxzz.elearing.course.entity.CurriculumSectionTopic;
import com.wxzz.elearing.course.entity.Exam;
import com.wxzz.elearing.course.entity.WxUserExamHistory;
import com.wxzz.elearing.course.service.IExamServiceService;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/wx-exam")
@Api(tags = "在线考试(小程序)")
public class WxExamController {

    @Autowired
    private IExamServiceService examServiceService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "查询该用户的在线考试", notes = "查询该用户的在线考试", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listExamByEnterpriseId", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<Exam>>> listExamByEnterpriseId(@RequestHeader String token, @RequestBody ExamByWxUserIdDto examByWxUserIdDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return examServiceService.listExamByWxUserId(jsonObject.getLong("id"), examByWxUserIdDto);
    }

    @ApiOperation(value = "查询考试详情", notes = "查询考试详情", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectExamDetailById/{examId}", method = RequestMethod.POST)
    public ReturnVO<Exam> selectExamDetailById(@PathVariable Long examId) {
        return examServiceService.selectExamDetailById(examId);
    }


    @ApiOperation(value = "展示我的考试记录", notes = "展示我的考试记录", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "wxListWxUserExamHistory", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<WxUserExamHistory>>> wxListWxUserExamHistory(@RequestHeader String token, @RequestBody WxUserExamHistoryDto wxUserExamHistoryDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return examServiceService.wxListWxUserExamHistory(jsonObject.getLong("id"), wxUserExamHistoryDto);
    }

    @ApiOperation(value = "根据考试记录id查询题目记录", notes = "根据考试记录id查询题目记录", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectCurriculumSectionTopicByExamHistoryId/{wxUserExamHistoryId}", method = RequestMethod.POST)
    public ReturnVO<List<CurriculumSectionTopic>> selectCurriculumSectionTopicByExamHistoryId(@PathVariable Long wxUserExamHistoryId) {
        return examServiceService.selectCurriculumSectionTopicByExamHistoryId(wxUserExamHistoryId);
    }



}
